package com.lgkj.view;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.adapter.CitySelectAdapter;
import com.lgkj.bean.CityBean;

import java.util.List;

/**
 * Created by Administrator on 2018/8/23 0023.
 */

public class SelectCityDialog extends Dialog {
    private Context context;
    private TextView tvTitle;
    private ListView lvCity;
    private String title;

    private CitySelectAdapter adapter;
    private CityBean.DataBean dataBean;
    private int type = 0;    //0  省   1  市   2  县

    public SelectCityDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        adapter = new CitySelectAdapter(context);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_city);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        tvTitle = findViewById(R.id.tv_title);
        lvCity = findViewById(R.id.lv_city);
        lvCity.setAdapter(adapter);
        setItemONClick();
    }

    public SelectCityDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public void setTitles(String title) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
    }

    public void setData(List<CityBean.DataBean> dataBeans, int type) {
        this.type = type;
        adapter.setDataBeans(dataBeans);
        adapter.notifyDataSetChanged();
    }

    private void setItemONClick() {
        lvCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                dataBean = adapter.getItem(i);
                resultBack.result(dataBean, type);
            }
        });
    }


    private ResultBack resultBack;

    public void setResult(ResultBack resultBack) {
        this.resultBack = resultBack;
    }

    public interface ResultBack {
        void result(CityBean.DataBean dataBean, int type);
    }
}
